home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / perlcl16.lha / perlclass1.6 / sample / chgfnt.c++ next >
C/C++ Source or Header  |  1992-10-28  |  4KB  |  130 lines

  1. #ifdef    TESTCHGFNT
  2. // Read an AmiPro style sheet and change any unknown font
  3. #include    <fstream.h>
  4. #include    <stdlib.h>
  5. #pragma hdrstop
  6.  
  7. #include    "perlclass.h"
  8. #include    "perlassoc.h"
  9.  
  10. void main(int argc, char **argv)
  11. {
  12. char c, *infn;
  13. ifstream ini("\\win3\\win.ini");
  14. ofstream fout("t.sty");
  15. int ln= 0;
  16. PerlString w;
  17. PerlStringList l;
  18. PerlStringList ttfonts;
  19. Assoc<PerlString> repfnts("", "");  // saves font replacement names
  20.  
  21.     if(argc < 2) infn= "test.sty";
  22.     else infn= argv[1];
  23.  
  24.     ifstream fin(infn);
  25.  
  26.     if(!ini){
  27.         cerr << "Can't open \\win3\\\win.ini" << endl;
  28.         exit(1);
  29.     }
  30.  
  31.     if(!fin){
  32.         cerr << "Can't open " << infn << endl;
  33.         exit(1);
  34.     }
  35.  
  36.     if(!fout){
  37.         cerr << "Can't open t.sty for write" << endl;
  38.         exit(1);
  39.     }
  40.  
  41.     cout << "Reading in truetype fonts" << endl;
  42.  
  43.     while(ini >> w){ // find the [fonts] section
  44.     if(w.m("\\[fonts\\]")) break;
  45.     }
  46.  
  47. //    cout << buf << endl;
  48.  
  49.     if(!ini.good()){ // checks all file state
  50.         cerr << "Couldn't find [fonts] section in win.ini" << endl;
  51.         exit(1);
  52.     }
  53.  
  54.     // make a list of truetype fonts
  55.     Regexp r1("^([a-zA-Z ]+) \\(([a-zA-Z ]+)\\)=");
  56.     Regexp r2("^TrueType$");
  57.     Regexp r3("\\[.*\\]");
  58.  
  59.     while(ini >> w){
  60.     if(w.m(r3)) break; // found the start of another section
  61.         if(w.m(r1, l) != 3) continue; // ignore this line
  62. //        cout << "Font match:" << l[1] << ", " << l[2] << endl;
  63.         if(l[2].m(r2)){
  64.             ttfonts.push(l[1]);
  65.         } 
  66.     }
  67.  
  68. //    cout << "ttfonts: " << endl << ttfonts << endl;
  69.     ini.close();
  70.  
  71.     cout << "Looking for non-truetype fonts" << endl;
  72.  
  73.     PerlString s, fnt, newfnt;
  74.     PerlStringList sl;
  75.     while(fin >> s){
  76.     ln++;
  77. //        cout << "line " << ln << ": <" << s << ">" << endl;
  78.         if(s.m("\\[fnt\\]")){
  79.             fout << s << endl; // write out [fnt] line
  80.                    // read next line which should have font in it
  81.         if(!(fin >> s)){
  82.         cerr << "Error reading font line " << ln << endl;
  83.         exit(1);
  84.         }
  85.         ln++;
  86.         sl= s.split(" \t");    // this extracts the font name
  87.             fnt= sl.join(" ");    // this puts the words back together
  88. //        cout << "font name: <" << fnt << ">" << endl;
  89.             if(!ttfonts.grep("^" + fnt + "$", "i")){ // not a truetype font
  90.                 int pos= s.index(fnt); // get position in string of font
  91.                 if(pos < 0){
  92.                     cerr << "Couldn't find <" << fnt << "> in string <" << s << "> line " << ln << endl;
  93.                     exit(1);
  94.                 }
  95.  
  96.                 // See if we already know what to exchange it with
  97.         if(repfnts.isin(fnt)) // just replace it
  98.             s.substr(pos, strlen(fnt)) = repfnts(fnt);
  99.                 else{ // need to ask what the new font name will be
  100.             again:
  101.                     cout << "Replace font <" << fnt << "> with:"; cout.flush();
  102.                     cin >> newfnt;
  103.                     if(!(sl=ttfonts.grep("^" + newfnt + "$", "i"))){
  104.                         cerr << "<" << newfnt << "> is not a valid font" << endl;
  105.                         goto again;
  106.                     }
  107.                     s.substr(pos, strlen(fnt)) = sl[0]; // replace it
  108.                     repfnts(fnt) = sl[0];  // remember for next time
  109.                 }
  110.         fout << s << endl;
  111.             }else{
  112. //                cout << fnt << " is a truetype font" << endl;
  113.                 fout << s << endl; // write out original font line
  114.             }
  115.  
  116.        }else{
  117.             fout << s << endl; // echo line out
  118.         }
  119.     }while(!fin.eof());
  120.  
  121.     cout << "replacement fonts were:" << endl << repfnts << endl;
  122.  
  123.     if(fout.fail())
  124.         cerr << "Something bad happened to the output file" << endl;
  125.     fout.close();
  126.  
  127. }
  128.  
  129. #endif
  130.